package leetcode_day._2021._202103;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author yzh
 * @data 2021/3/23 15:18
 */
public class _23_341 {
}

class NestedIterator implements Iterator<Integer> {
    List<Integer> list;
    Iterator<Integer> curr;

    public NestedIterator(List<NestedInteger> nestedList) {
        list = new ArrayList<>();
        dfs(nestedList);
        curr = list.iterator();
    }

    public void dfs(List<NestedInteger> nestedList) {
        for (NestedInteger nestedInteger : nestedList) {
            if (nestedInteger.isInteger()) list.add(nestedInteger.getInteger());
            else dfs(nestedInteger.getList());
        }
    }

    @Override
    public Integer next() {
        return curr.next();
    }

    @Override
    public boolean hasNext() {
        return curr.hasNext();
    }
}

interface NestedInteger {
    // @return true if this NestedInteger holds a single integer, rather than a nested list.
    public boolean isInteger();

    // @return the single integer that this NestedInteger holds, if it holds a single integer
    // Return null if this NestedInteger holds a nested list
    public Integer getInteger();

    // @return the nested list that this NestedInteger holds, if it holds a nested list
    // Return null if this NestedInteger holds a single integer
    public List<NestedInteger> getList();
}
